package com.atguigu.common.vaild;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.HashSet;
import java.util.Set;

/**
 * @author sourire
 * @version 1.0
 * @date 2022/1/20 16:10
 */
public class ListValueConstraintValidator implements ConstraintValidator<ListValue,Integer> {
    private Set<Integer> valSet = new HashSet<>();
    //初始化
    @Override
    public void initialize(ListValue constraintAnnotation) {
        int[] vals = constraintAnnotation.vals();
        for (int val : vals) {
            valSet.add(val);
        }
    }

    //校验
    @Override
    public boolean isValid(Integer value, ConstraintValidatorContext context) {
        return valSet.contains(value);
    }
}
